package com.blog.dao.impl;

import com.blog.dao.BaseDao;
import com.blog.dao.AxisDao;
import com.blog.pojo.Article;
import com.blog.utils.SqlIUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AxisDaoImpl extends BaseDao implements AxisDao {

    @Override
    public List<String> findAllYear() {
        List<String> yearList = new ArrayList<String>();
        try {
            String sql = "select  distinct  year(time)  from  tb_article ";
            ResultSet resultSet = this.execQuery(sql, null);
            while (resultSet.next()) {
                String  year = resultSet.getString(1);
                yearList.add(year);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConn(conn, resultSet, ps);
        }
        return yearList;
    }

    @Override
    public List<Article> findAllArticleByYear(String year) {
        List<Article> articleList = new ArrayList<Article>();
        if(SqlIUtils.isSqlValid(year)){
            return articleList;
        }
        try{
            String sql = "select *  from  tb_article  where  YEAR(time)= ?  ";
        Object[] parms = {year};
        ResultSet resultSet = this.execQuery(sql,parms);
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String title = resultSet.getString(2);
            String author = resultSet.getString(3);
            String sort = resultSet.getString(4);
            String time = resultSet.getString(5);
            Integer star = resultSet.getInt(6);
            Integer comment = resultSet.getInt(7);
            Integer visit = resultSet.getInt(8);
            String content = resultSet.getString(9);
            Article article = new Article(id, title, author, sort, time, star, comment, visit, content);
            articleList.add(article);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        this.closeConn(conn, resultSet, ps);
    }
        return articleList;
    }
}
